
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:ymt/ui/preview.dart';
import 'nav.dart';

class CacheImage{

  static BaseCacheManager sCacheManger = DefaultCacheManager();

  static Image load(url) {
    return Image(image: CachedNetworkImageProvider(url),frameBuilder: (BuildContext context, Widget child, int frame, bool wasSynchronouslyLoaded){
      return Container(child: child,decoration: BoxDecoration(color: Colors.black38),);
    },);
  }

  static Image loadImageWithFit(url, fit) {
    return Image(image: CachedNetworkImageProvider(url),fit: fit,);
  }

  static GestureDetector imageClickToDetails(context, imgUrl, products, position){
    return GestureDetector(child: CacheImage.load(imgUrl), onTap: () =>{
      Nav.wrapNav(context, DetailsPage(products, position, context))
    },);
  }

  static download(url) async {
    FileInfo cacheFile = await sCacheManger.getFileFromCache(url);
    if(cacheFile != null){
      return cacheFile.file;
      print("$url已缓存，路径位于${cacheFile.file.path}");
    }else{
      var file = await sCacheManger.getSingleFile(url);
      if(file == null){
        print("$url缓存失败");
      }else{
        print("$url缓存成功，路径位于${file.path}");
      }
      return file;
    }

  }
}